﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvDTE;

namespace devtm.Editor.TypeSelector.ParsingSolution
{


    public class SolutionItem
    {


        protected EnvDTE.Project project;

        public SolutionItem()
        {

        }

        protected Dictionary<string, Property> _properties = null;
        protected Property FindProperty(string p)
        {
            if (_properties == null)
                BuildProperties();

            return _properties[p];

        }

        protected virtual void BuildProperties()
        {
            _properties = new Dictionary<string, Property>();

            foreach (Property item in project.Properties)
                _properties.Add(item.Name, item);

        }

        public SolutionItem(EnvDTE.Project project)
        {
            // TODO: Complete member initialization
            this.project = project;
        }


        public virtual IEnumerable<T> GetItem<T>() where T : SolutionItem
        {


            foreach (EnvDTE.ProjectItem s in project.ProjectItems)
            {

                SolutionItem fld = null;

                EnvDTE.Project proj = s.SubProject as EnvDTE.Project;
                if (proj != null)
                    fld = new Project(proj);

                else if (s.Kind == "{66A26720-8FB5-11D2-AA7E-00C04F688DDE}")
                    fld = new FolderSolution(s as EnvDTE.Project);

                else if (s.Kind == "{EA6618E8-6E24-4528-94BE-6889FE16485C}")
                    fld = new VirtualFolder(s as EnvDTE.Project);

                else
                    fld = new Item(s);



                if (fld is T)
                    yield return fld as T;

                foreach (SolutionItem i2 in fld.GetItem<T>())
                    yield return i2 as T;

            }



        }


        public virtual string Kind
        {
            get
            {
                return project.Kind;
            }
        }


        public virtual string Name
        {
            get
            {
                return project.Name;
            }
            set
            {
                project.Name = value;
            }
        }


    }


}
